//相关数据结构和接口请参考 https://192.168.1.99/svn/MIDISV3/Software/trunk/public/inf/devModel
const mediaModule = {
  namespaced: true,
  state: {
    inputDev: {
      cnt: 0,
      devs: []
    },
    inputZsDev: {
      cnt: 0,
      devs: []
    },
    inputChannel: {
      cnt: 0,
      channels: []
    },
    inputChannelExam: {
      channels: []
    },
    outputDev: {
      cnt: 0,
      devs: []
    },
    outputZsDev: {
      cnt: 0,
      devs: []
    },
    outputChannel: {
      cnt: 0,
      channels: []
    },
    streamPullInfo: {
      maxPullCh: 0,
      chs: []
    },
    streamPushInfo: {
      maxPushCh: 0,
      chs: []
    },
    audioOutPutInfo: {
      cnt: 0,
      audios: []
    },
    videoSwitchInfo: {
      cnt: 0,
      chs: []
    },
    layoutSwitchInfo: {
      cnt: 0,
      chs: []
    },
    srcName: '',
    multicase: {},
    hideMulticast: false,
    hideInputDev: false,
    hideInputChn: false,
    hideOutputDev: false,
    hideOutputChn: false,
  },
  mutations: {
    GET_INPUT_DEVCNT(state, res) {
      state.inputDev.cnt = res
    },
    GET_INPUT_ZSDEVCNT(state, res) {
      state.inputZsDev.cnt = res
    },
    GET_INPUT_DEVINFO(state, res) {
      if (res) {
        if (state.inputDev.devs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.inputDev.devs[res.index][name] = res[name]
          }
        } else {
          state.inputDev.devs[res.index] = res;
          state.inputDev.devs.splice(res.index, 1, res);
          // state.inputDev.devs.push(res)
        }  
        // state.inputDev.devs[res.index] = res;
        // state.inputDev.devs.splice(res.index, 1, res);      
      } else {
        // state.inputDev.devs = []
        state.inputDev.devs.splice(state.inputDev.devs.length)
      }      
    },
    GET_INPUT_ZSDEVINFO(state, res) {
      if (res) {
        if (state.inputZsDev.devs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.inputZsDev.devs[res.index][name] = res[name]
          }
        } else {
          state.inputZsDev.devs[res.index] = res;
          state.inputZsDev.devs.splice(res.index, 1, res);
          // state.inputZsDev.devs.push(res)
        }
        
      } else {
        state.inputZsDev.devs = []
      }      
    },
    GET_LOOPBACK(state, res) {
      if (res) {
        if (state.inputDev.devs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.inputDev.devs[res.index][name] = res[name]
          }
        } else {
          state.inputDev.devs.push(res)
        }
      } else {
        state.inputDev.devs = []
      }
    },
    GET_INPUT_CHCNT(state, res) {
      state.inputChannel.cnt = res
    },
    GET_INPUT_CHANNELINFO(state, res) {
      if (res) {
        if (state.inputChannel.channels[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.inputChannel.channels[res.index][name] = res[name]
          }
        } else {
          state.inputChannel.channels.push(res)
        }
      } else {
        state.inputChannel.channels = []
      }
    },
    GET_INPUT_SUBTITLE(state, res) {

      if (res) {
        if (state.inputChannel.channels[res.index]) {
          if (state.inputChannel.channels[res.index].subtitle) {
            if (state.inputChannel.channels[res.index].subtitle[res.subtitle.index]) {
              state.inputChannel.channels[res.index].subtitle.splice(res.subtitle.index)//使用splice更新数组使得vue能够监听到数据变化
              state.inputChannel.channels[res.index].subtitle[res.subtitle.index] = res.subtitle
            } else {
              state.inputChannel.channels[res.index].subtitle.push(res.subtitle)
            }
          } else {
            state.inputChannel.channels[res.index].subtitle = []
            state.inputChannel.channels[res.index].subtitle.push(res.subtitle)
          }
        } else {
          state.inputChannel.channels.push(res)
        }
      } else {
        state.inputChannel.channels = []
      }
    },
    GET_OUTPUT_SUBTITLE(state, res) {

      if (res) {
        if (state.outputChannel.channels[res.index]) {
          if (state.outputChannel.channels[res.index].subtitle) {
            if (state.outputChannel.channels[res.index].subtitle[res.subtitle.index]) {
              state.outputChannel.channels[res.index].subtitle.splice(res.subtitle.index)
              state.outputChannel.channels[res.index].subtitle[res.subtitle.index] = res.subtitle
            } else {
              state.outputChannel.channels[res.index].subtitle.push(res.subtitle)
            }
          } else {
            state.outputChannel.channels[res.index].subtitle = []
            state.outputChannel.channels[res.index].subtitle.push(res.subtitle)
          }
        } else {
          state.outputChannel.channels.push(res)
        }
      } else {
        state.outputChannel.channels = []
      }
    },
    GET_INPUT_STREAM(state, res) {
      if (res) {
        if (state.inputChannel.channels[res.index]) {
          if (state.inputChannel.channels[res.index].stream) {
            if (state.inputChannel.channels[res.index].stream[res.stream.index]) {
              for (var proptyName in res.stream) {
                state.inputChannel.channels[res.index].stream[res.stream.index][proptyName] = res.stream[proptyName]
              }
            } else {
              state.inputChannel.channels[res.index].stream.push(res.stream)
            }
          } else {
            state.inputChannel.channels[res.index].stream = []
            state.inputChannel.channels[res.index].stream.push(res.stream)
          }
        } else {
          state.inputChannel.channels.push(res)
        }
      } else {
        state.inputChannel.channels = []
      }
    },
    GET_OUTPUT_DEVCNT(state, res) {
      state.outputDev.cnt = res
    },
    GET_OUTPUT_ZSDEVCNT(state, res) {
      state.outputZsDev.cnt = res
    },
    GET_OUTPUT_DEVINFO(state, res) {      
      if (res) {
        if (state.outputDev.devs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.outputDev.devs[res.index][name] = res[name]
          }
        } else {
          state.outputDev.devs[res.index] = res;
          state.outputDev.devs.splice(res.index, 1, res);
          // state.outputDev.devs.push(res)
        }
        
      } else {
        state.outputDev.devs = []
      }
      
    },
    GET_OUTPUT_ZSDEVINFO(state, res) {      
      if (res) {
        if (state.outputZsDev.devs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.outputZsDev.devs[res.index][name] = res[name]
          }
        } else {
          // state.outputZsDev.devs.push(res)
          state.outputZsDev.devs[res.index] = res;
          state.outputZsDev.devs.splice(res.index, 1, res);
        }
        
      } else {
        state.outputZsDev.devs = []
      }
      
    },
    GET_OUTPUT_CHCNT(state, res) {
      state.outputChannel.cnt = res
    },
    GET_OUTPUT_CHANNELINFO(state, res) {
      if (res) {        
        if (res.hasOwnProperty('subtitle')) {
          if (state.outputChannel.channels[res.index]) {
            if (state.outputChannel.channels[res.index].subtitle) {
              if (state.outputChannel.channels[res.index].subtitle[res.subtitle.index]) {
                state.outputChannel.channels[res.index].subtitle[res.subtitle.index] = res.subtitle
              } else {
                state.outputChannel.channels[res.index].subtitle.push(res.subtitle)
              }
            } else {
              state.outputChannel.channels[res.index].subtitle = []
              state.outputChannel.channels[res.index].subtitle.push(res.subtitle)
            }
          } else {
            var req = []
            req.push(res.subtitle)
            state.outputChannel.channels.push(req)
          }
        } else {
          if (state.outputChannel.channels[res.index]) {
            //动态新增属性并赋值
            for (var name in res) {
              state.outputChannel.channels[res.index][name] = res[name]
            }
          } else {
            state.outputChannel.channels.push(res)
          }
        }
      } else {
        state.outputChannel.channels = []
      }
    },
    GET_MULTICASE_INFO(state, res) {
      state.multicase = res
    },
    HIDE_MULTICAST(state, res) {
      state.hideMulticast = res
    },
    GET_STREAMPULL_CNT(state, res) {
      state.streamPullInfo.maxPullCh = res
    },
    GET_STREMPULL_INFO(state, res) {
      if (res) {
        if (state.streamPullInfo.chs[res.index]) {
          for (var name in res) {
            state.streamPullInfo.chs[res.index][name] = res[name]
          }
        } else {
          if (!res.hasOwnProperty('status')){
            res.status = ''
          }
          if (!res.hasOwnProperty('url')){
            res.url = ''
          }
          state.streamPullInfo.chs.push(res)
        }
      } else {
        state.streamPullInfo.chs.splice(0, state.streamPullInfo.chs.length);
      }
      
    },
    GET_STREMPUSH_INFO(state, res) {
      if (res) {
        if (state.streamPushInfo.chs[res.index]) {
          //动态新增属性并赋值
          for (var name in res) {
            state.streamPushInfo.chs[res.index][name] = res[name]
          }
        } else {
          if (!res.hasOwnProperty('status')) {
            res.status = ''
          }
          if (!res.hasOwnProperty('url')) {
            res.url = ''
          }
          if (!res.hasOwnProperty('reconnect')) {
            res.reconnect = false
          }
          state.streamPushInfo.chs.push(res)
        }
      } else {
        state.streamPushInfo.chs = []
      }
      // if (res) {
      //   state.streamPushInfo.chs.splice(res.index, 1, res);
      // } else {
      //   state.streamPushInfo.chs.splice(res.index, state.streamPushInfo.chs.length);
      // }
    },
    GET_AUDIOOUT_CNT(state, res) {
      state.audioOutPutInfo.cnt = res
    },
    GET_AUDIOOUT_INFO(state, res) {
      if (res) {
        state.audioOutPutInfo.audios[res.index] = res;
        state.audioOutPutInfo.audios.splice(res.index, 1, res);
      } else {
        state.audioOutPutInfo.audios.splice(res.index, state.audioOutPutInfo.audios.length);
      }
    },
    GET_VIDEOSWITCH_CNT(state, res) {
      state.videoSwitchInfo.cnt = res
    },
    GET_VIDEOSWITCH_INFO(state, res) {
      if (res) {
        state.videoSwitchInfo.chs[res.index] = res;
        state.videoSwitchInfo.chs.splice(res.index, 1, res);
      } else {
        state.videoSwitchInfo.chs.splice(res.index, state.videoSwitchInfo.chs.length);
      }
    },
    GET_LAYOUTSWITCH_CNT(state, res) {
      state.layoutSwitchInfo.cnt = res
    },
    GET_LAYOUTSWITCH_INFO(state, res) {
      if (res) {
        state.layoutSwitchInfo.chs[res.index] = res;
        state.layoutSwitchInfo.chs.splice(res.index, 1, res);
      } else {
        state.layoutSwitchInfo.chs.splice(res.index, state.layoutSwitchInfo.chs.length);
      }
    },
    SET_SRCNAME(state, res) {
      state.srcName = res
    },
  },
  actions: {
    getInputDevCnt({
      commit
    }, res) {
      commit('GET_INPUT_DEVCNT', res);
    },
    getInputZsDevCnt({
      commit
    }, res) {
      commit('GET_INPUT_ZSDEVCNT', res);
    },
    getInputDevInfo({
      commit
    }, res) {
      commit('GET_INPUT_DEVINFO', res);
    },
    getInputZsDevInfo({
      commit
    }, res) {
      commit('GET_INPUT_ZSDEVINFO', res);
    },
    getInputChCnt({
      commit
    }, res) {
      commit('GET_INPUT_CHCNT', res);
    },
    getInputChannelInfo({
      commit
    }, res) {
      commit('GET_INPUT_CHANNELINFO', res);
    },
    getOutputDevCnt({
      commit
    }, res) {
      commit('GET_OUTPUT_DEVCNT', res);
    },
    getOutputDevInfo({
      commit
    }, res) {
      commit('GET_OUTPUT_DEVINFO', res);
    },
    getOutputZsDevCnt({
      commit
    }, res) {
      commit('GET_OUTPUT_ZSDEVCNT', res);
    },
    getOutputZsDevInfo({
      commit
    }, res) {
      commit('GET_OUTPUT_ZSDEVINFO', res);
    },
    getOutputChCnt({
      commit
    }, res) {
      commit('GET_OUTPUT_CHCNT', res);
    },
    getOutputChannelInfo({
      commit
    }, res) {
      commit('GET_OUTPUT_CHANNELINFO', res);
    },
    getInputSubtitle({
      commit
    }, res) {
      commit('GET_INPUT_SUBTITLE', res);
    },
    getOutputSubtitle({
      commit
    }, res) {
      commit('GET_OUTPUT_SUBTITLE', res);
    },
    getInputStream({
      commit
    }, res) {
      commit('GET_INPUT_STREAM', res);
    },
    getMulticaseInfo({
      commit
    }, res) {
      commit('GET_MULTICASE_INFO', res);
    },
    getLoopBack({
      commit
    }, res) {
      commit('GET_LOOPBACK', res);
    },
    hideMulticast({
      commit
    }, res) {
      commit('HIDE_MULTICAST', res);
    },
    getStreamPullCnt({
      commit
    }, res) {
      commit('GET_STREAMPULL_CNT', res);
    },
    getStremPullInfo({
      commit
    }, res) {
      commit('GET_STREMPULL_INFO', res);
    },
    getStreamPushCnt({
      commit
    }, res) {
      commit('GET_STREAMPULL_CNT', res);
    },
    getStremPushInfo({
      commit
    }, res) {
      commit('GET_STREMPUSH_INFO', res);
    },
    getAudioOutCnt({
      commit
    }, res) {
      commit('GET_AUDIOOUT_CNT', res);
    },
    getAudioOutInfo({
      commit
    }, res) {
      commit('GET_AUDIOOUT_INFO', res);
    },
    getVideoSwitchCnt({
      commit
    }, res) {
      commit('GET_VIDEOSWITCH_CNT', res);
    },
    getVideoSwitchInfo({
      commit
    }, res) {
      commit('GET_VIDEOSWITCH_INFO', res);
    },
    getLayoutSwitchCnt({
      commit
    }, res) {
      commit('GET_LAYOUTSWITCH_CNT', res);
    },
    getLayoutSwitchInfo({
      commit
    }, res) {
      commit('GET_LAYOUTSWITCH_INFO', res);
    },
    setSrcName({
      commit
    }, res) {
      commit('SET_SRCNAME', res);
    },
  }
};

export default mediaModule
